package com.example.csetpp.controller;


import com.example.csetpp.pojo.dto.PageUserDTO;
import com.example.csetpp.pojo.dto.file.*;
import com.example.csetpp.pojo.dto.user.SendCodeDTO;
import com.example.csetpp.service.FileService;
import com.example.csetpp.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@Tag(name = "文件相关")
@RequestMapping("/user/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @Operation(summary = "上传头像")
    @PostMapping("/avatar")
    public Result<?> postAvatar(@RequestBody MultipartFile image, String userId) throws Exception {

        String url = fileService.postAvatar(image, userId);
        return Result.success(url);
    }

    @Operation(summary = "删除文件")
    @PostMapping("/delete-file")
    public Result<?> deleteFile(@RequestBody FileUrlDTO fileUrlDTO) throws Exception {
        String url = fileUrlDTO.getUrl();
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String result = fileService.deleteFile(url, fileName);
        return Result.success(result);
    }

    @Operation(summary = "股票预测")
    @PostMapping("/stock-predict")
    public Result<?> stockPredict(@RequestBody StockDTO stockDTO) throws Exception {

        return Result.success(fileService.stockPredict(stockDTO));
    }

    @Operation(summary = "用电数据预测")
    @PostMapping("/electricity-predict")
    public Result<?> electricityPredict(@RequestBody StaticPredictDTO staticPredictDTO) throws Exception {

        return Result.success(fileService.electricityPredict(staticPredictDTO));
    }

    @Operation(summary = "静态天气预测")
    @PostMapping("/static_weather-predict")
    public Result<?> staticWeatherPredict(@RequestBody StaticPredictDTO staticPredictDTO) throws Exception {
        return Result.success(fileService.staticWeatherPredict(staticPredictDTO, 0));
    }


    @Operation(summary = "用户上传天气文件预测")
    @PostMapping("/submit-weather-predict")
    public Result<?> submitWeatherPredict(@RequestBody MultipartFile file, String userId, Integer time_step) throws Exception {
        return Result.success(fileService.submitWeatherPredict(file, userId, time_step));
    }

    @Operation(summary = "根据用户id获得历史文件记录")
    @PostMapping("/get-file-record")
    public Result<?> getFileRecord(@RequestBody PageUserDTO pageUserDTO) {
        return Result.success(fileService.getFileRecord(pageUserDTO));
    }

    @Operation(summary = "删除用户文件历史记录")
    @PostMapping("/delete-user-file")
    public Result<?> deleteUserFile(@RequestBody Map<String, String> payload) throws Exception {
        return Result.success(fileService.deleteUserFile(payload.get("id")));
    }

    @Operation(summary = "用户预测记录查询")
    @PostMapping("/user-prediction-search")
    public Result<?> UserPredictionSearch(@RequestBody UserPredictionSearchDTO userPredictionSearchDTO) {
        return Result.success(fileService.UserPredictionSearch(userPredictionSearchDTO));
    }

    @Operation(summary = "后台模型预测数据获取")
    @PostMapping("/get-module-prediction")
    public Result<?> getModulePrediction(@RequestBody ModulePredictionDTO modulePredictionDTO) {
        return Result.success(fileService.getModulePrediction(modulePredictionDTO));
    }

    @Operation(summary = "后台传入日期手动预测某天的数据")
    @PostMapping("/predictByDate")
    public Result<?> predictByDate(@RequestBody ModulePredictionDTO modulePredictionDTO) {
        fileService.predictByDate(modulePredictionDTO);
        return Result.success("OK");
    }

    @Operation(summary = "根据键值删除历史预测")
    @PostMapping("/delete-prediction-byKey")
    public Result<?> deletePredictionByKey(@RequestBody KeyDTO keyDTO) {
        fileService.deletePredictionByKey(keyDTO);
        return Result.success("OK");
    }


}
